package org.ggumm21.gae;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.hadoop.mapreduce.JobID;

public class JobContext {

	  // Current request
	  private final HttpServletRequest request;
	  private final String jobId;

	  static final String JOB_ID_PARAMETER_NAME = "jobID";

	  JobContext(HttpServletRequest request, boolean startJob) {
		    this(request, startJob ? generateNewJobID() : "");
		  }

	  JobContext(HttpServletRequest request, String jobId) {
	    this.request = request;
	    this.jobId = jobId;
	  }

	  protected static String generateNewJobID() {
		    return 
		        ("" + System.currentTimeMillis() + UUID.randomUUID()).replace("-", "");
		  }

	  static String getJobIDFromRequest(HttpServletRequest req) {
		    String jobIdString = req.getParameter(JOB_ID_PARAMETER_NAME);
		    if (jobIdString == null) {
		      throw new RuntimeException("Couldn't get Job ID for request. Aborting!");
		    }
		    return jobIdString;
		  }
}
